package cn.edu.cque.mall.controller;

import cn.edu.cque.mall.entity.Category;
import cn.edu.cque.mall.entity.InfoResult;
import cn.edu.cque.mall.entity.Order;
import cn.edu.cque.mall.entity.Product;
import cn.edu.cque.mall.service.CategoryService;
import cn.edu.cque.mall.service.OrderItemService;
import cn.edu.cque.mall.service.OrderService;
import cn.edu.cque.mall.service.ProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminController
 * @Description
 * @Author chen
 * @Date 2020/10/12 0012 9:40
 */
@Controller
@RequestMapping("/admin")
public class AdminController{

    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductService productService;

    @Autowired
    OrderService orderService;

    @Autowired
    OrderItemService OrderItemService;

    @RequestMapping("/category/list")
    public ModelAndView findAll() {
        ModelAndView modelAndView = new ModelAndView();
        List<Category> categoryList = categoryService.findAll();
        categoryList.forEach(System.out::println);
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("/admin/category-list");
        return modelAndView;
    }

    @RequestMapping("/category/updates")
    @ResponseBody
    public InfoResult updatesCategory(String cid, String cname) {
        Category category = new Category();
        category.setCid(cid);
        category.setCname(cname);
        System.out.println(cid + "--" + cname);
        categoryService.updates(category);
        return InfoResult.OK();
    }

    @RequestMapping("/category/del")
    @ResponseBody
    public InfoResult delCategory(String cid) {
        System.out.println(cid);
        System.out.println("-------");
        categoryService.delByCid(cid);
        return InfoResult.OK();
    }

    @RequestMapping("/category/add")
    @ResponseBody
    public InfoResult addCategory(Category category) {
        categoryService.addCategory(category);
        return InfoResult.OK();
    }


    @RequestMapping("/product/list")
    public ModelAndView productFindAll(String pageNum, String pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("pageSize" + pageSize);
        if (pageSize == null) {
            pageSize = "4";
        }
        System.out.println("pageSize" + pageSize);

        if (pageNum == null) {
            pageNum = "1";
        }
        PageInfo productList = productService.findPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        modelAndView.addObject("productList", productList);
        modelAndView.setViewName("/admin/product-list");
        return modelAndView;
    }


    @RequestMapping("/product/updates")
    @ResponseBody
    public InfoResult updatesProduct(HttpServletRequest request) {
        Product product = new Product();
        product.setId(request.getParameter("pid"));
        product.setName(request.getParameter("pname"));
        product.setMarketPrice(Double.parseDouble(request.getParameter("market_price")));
        product.setShopPrice(Double.parseDouble(request.getParameter("shop_price")));
        product.setImage(request.getParameter("pimage"));
        String pdate = request.getParameter("pdate");
        Date pdates = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            pdates = format.parse(pdate);
            System.out.println(pdates);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        product.setCreateDate(pdates);
        product.setIsHot(Integer.parseInt(request.getParameter("isHot")));
        product.setDesc(request.getParameter("pdesc"));
        product.setFlag(Integer.parseInt(request.getParameter("pflag")));
        product.setCid(request.getParameter("cid"));
        productService.updates(product);
        return InfoResult.OK();
    }

    @RequestMapping("/product/del")
    @ResponseBody
    public InfoResult delProduct(String pid) {
        productService.delByPid(pid);
        return InfoResult.OK();
    }


    @RequestMapping("/product/add")
    @ResponseBody
    public InfoResult addProduct(HttpServletRequest request) {
        String pid = request.getParameter("pid");
        String pname = request.getParameter("pname");
        double market_price = Double.parseDouble(request.getParameter("market_price"));
        double shop_price = Double.parseDouble(request.getParameter("shop_price"));
        String pimage = request.getParameter("pimage");
        String pdate = request.getParameter("pdate");
        int isHot = Integer.parseInt(request.getParameter("isHot"));
        int pflag = Integer.parseInt(request.getParameter("pflag"));
        String pdesc = request.getParameter("pdesc");
        String cid = request.getParameter("cid");

        Product product = new Product();
        product.setId(pid);
        product.setName(pname);
        product.setMarketPrice(market_price);
        product.setShopPrice(shop_price);
        product.setImage(pimage);
        product.setCreateDate(new Date());
        product.setIsHot(isHot);
        product.setDesc(pdesc);
        product.setFlag(pflag);
        product.setCid(cid);
        productService.addProduct(product);
        return InfoResult.OK();
    }


    @RequestMapping("/order/list")
    public ModelAndView orderFindAll() {
        ModelAndView modelAndView = new ModelAndView();
        List<Order> orderList = orderService.findAll();
        modelAndView.addObject("orderList", orderList);
        modelAndView.setViewName("/admin/order-list");
        return modelAndView;
    }

    @RequestMapping("/order/del")
    @ResponseBody
    public InfoResult delOrder(String oid) {
        System.out.println(oid);
        OrderItemService.delByOid(oid);
        orderService.delByOid(oid);
        return InfoResult.OK();
    }

    @RequestMapping("/order/updates")
    @ResponseBody
    public InfoResult updatesOrder(String oid,String name,String address,String telephone) {
        Order order = new Order();
        order.setName(name);
        order.setAddress(address);
        order.setTelephone(telephone);
        orderService.updateOrder(name, address, telephone, oid);
        return InfoResult.OK();
    }


}
